package com.cluster.platform.service.impl;

import com.cluster.platform.common.api.CommonPage;
import com.cluster.platform.common.api.CommonResult;
import com.cluster.platform.mapper.CmsCommentsReplyMapper;
import com.cluster.platform.model.CmsCommentsReply;
import com.cluster.platform.service.CmsCommentsReplyService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: CmsCommentsReplyServiceImpl
 */
@Service
public class CmsCommentsReplyServiceImpl implements CmsCommentsReplyService {

    @Autowired
    private CmsCommentsReplyMapper cmsCommentsReplyMapper;

    /**
     * 新增回复
     *
     * @param cmsCommentsReply
     */
    @Override
    @Transactional
    public CommonResult<Object> addCommentsReply(CmsCommentsReply cmsCommentsReply) {
        if (null == cmsCommentsReply) {
            return CommonResult.failed("未获取到参数信息");
        }
        if (StringUtils.isBlank(cmsCommentsReply.getReplyId())) {
            return CommonResult.failed("主题id为空");
        }
        cmsCommentsReply.setId(String.valueOf(System.currentTimeMillis()));
        cmsCommentsReply.setInsertDate(new Date());
        cmsCommentsReply.setStatus(1);
        int insertSelective = cmsCommentsReplyMapper.insertSelective(cmsCommentsReply);
        if (insertSelective < 0) {
            return CommonResult.failed("新增回复失败");
        }
        return CommonResult.success(cmsCommentsReply);
    }

    /**
     * 查询所有回复信息
     *
     * @param cmsCommentsReply
     */
    @Override
    public CommonResult<Object> searchCommentsReply(CmsCommentsReply cmsCommentsReply) {
        if (null == cmsCommentsReply) {
            return CommonResult.failed("未获取到参数信息");
        }
        List<CmsCommentsReply> cmsCommentsReplies = cmsCommentsReplyMapper.searchCommentsReplyList(cmsCommentsReply);
        if (null == cmsCommentsReplies || cmsCommentsReplies.isEmpty()) {
            return CommonResult.failed("未查询到数据");
        }
        return CommonResult.success(cmsCommentsReplies);
    }

    /**
     * 根据条件分页查询
     *
     * @param cmsCommentsReply
     * @param pageNum
     * @param pageSize
     */
    @Override
    public CommonResult<Object> searchCommentsReplyPageBy(CmsCommentsReply cmsCommentsReply, Integer pageNum, Integer pageSize) {
        if (null == cmsCommentsReply) {
            return CommonResult.failed("未获取到参数信息");
        }
        if (null == pageNum || null == pageSize) {
            return CommonResult.failed("未获取到分页参数信息");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<CmsCommentsReply> cmsCommentsReplies = cmsCommentsReplyMapper.searchCommentsReplyList(cmsCommentsReply);
        if (null == cmsCommentsReplies || cmsCommentsReplies.isEmpty()) {
            return CommonResult.failed("未查询到信息");
        }
        return CommonResult.success(CommonPage.restPage(cmsCommentsReplies));
    }

    /**
     * 删除回复-逻辑
     *
     * @param id
     */
    @Override
    @Transactional
    public CommonResult<Object> deleteCommentsReply(String id) {
        if (StringUtils.isBlank(id)) {
            return CommonResult.failed("未获取到参数信息");
        }
        CmsCommentsReply cmsCommentsReply = new CmsCommentsReply();
        cmsCommentsReply.setId(id);
        cmsCommentsReply.setStatus(0);
        cmsCommentsReply.setUpdateDate(new Date());
        int update = cmsCommentsReplyMapper.updateByPrimaryKeySelective(cmsCommentsReply);
        if (update < 0) {
            return CommonResult.failed("删除失败");
        }
        return CommonResult.success(cmsCommentsReply);
    }

    /**
     * 批量删除
     *
     * @param cmsCommentsReplies
     */
    @Override
    @Transactional
    public CommonResult<Object> batchDeleteCommentsReply(List<CmsCommentsReply> cmsCommentsReplies) {
        if (null == cmsCommentsReplies || cmsCommentsReplies.isEmpty()) {
            return CommonResult.failed("未获取参数信息");
        }
        int batch = cmsCommentsReplyMapper.batchDeleteCommentsReply(cmsCommentsReplies);
        if (batch < 0) {
            return CommonResult.failed("批量删除失败");
        }
        return CommonResult.success(batch);
    }
}
